package com.hzspace.advice.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2020/2/28_21:31
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

//    @ExceptionHandler(value = Throwable.class)
//    public void exception(Throwable t, HttpServletResponse response) throws IOException {
//        t.printStackTrace();
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter writer = response.getWriter();
//        writer.append("System Busy!");
//        writer.flush();
//        writer.close();
//    }

//    @ExceptionHandler(value = Throwable.class)
//    @ResponseBody
//    public Object exception(Throwable t){
//        t.printStackTrace();
//        return "System Busy!";
//    }

    @ExceptionHandler(value = Throwable.class)
    public ModelAndView exception(Throwable t){
        t.printStackTrace();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errorMsg","System Busy!");
        modelAndView.setViewName("error");
        return modelAndView;
    }

}
